package view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.engine.SpaceJustifier;
	import flash.utils.ByteArray;
	
	import utils.PNGEncoder;
	
	public class EditSpaceItem extends Sprite
	{
		private var loader:Loader;
		private var ew:int = 444;
		private var eh:int = 444;
		private var color:String;
		public var currentConsole:Console;
		private var consoleAry:Array = new Array;
		public var content:Sprite;
		public var data:Object;
		public function EditSpaceItem()
		{
			init();
		}
		private function init():void{
			loader = new Loader;
			this.addChild(loader);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioerror);
			loader.addEventListener(MouseEvent.CLICK,onLoaderClick);
			content = new Sprite;
			this.addChild(content);
		}
		public function setMarkColor(mark:String,color:String):void{
			if(this.color != color){
				loader.unload();
				loader.scaleX = loader.scaleY = 1;
				loader.load(new URLRequest("assets/pic/" + color + "-" + mark + ".jpg"));
				this.color = color;
			}
		}
		private function onLoadComplete(event:Event):void{
			var w:int = loader.width;
			var h:int = loader.height;
			var sx:Number = ew/w;
			var sy:Number = eh/h;
			if(sx<=sy && sx<1){
				loader.scaleX = loader.scaleY = sx;
			}
			if(sy<=sx && sy<1){
				loader.scaleX = loader.scaleY = sy;
			}
			
			loader.x  = (ew - loader.width)/2;
			loader.y  = (eh - loader.height)/2;
			
		}
		private function ioerror(event:IOErrorEvent):void{
			trace(event)
		}
		public function addTxt(maskSp:DisplayObject):Console{
			var txt:TextSprite = new TextSprite;
			var console:Console = new Console(txt,false,maskSp);
			content.addChild(console)
			consoleAry.push(console);
			console.addEventListener(MouseEvent.CLICK,onConsoleClick);
			if(currentConsole){
				currentConsole.select = false;
			}
			currentConsole = console;
			console.x = ew/2;
			console.y = eh/2;
			return console;
		}
		public function addBitmap(btimap:Bitmap,maskSp:DisplayObject,url:Object,scale:Number,lev:int):Console{
			if(lev == 0){
				for(var i:int;i<consoleAry.length;i++){
					if(Console(consoleAry[i]).single){
						Console(consoleAry[i]).onDel();
					}
				}
			}
			var console:Console = new Console(btimap,true,maskSp,url,false);
			console.ishot = lev;
			if(lev == 0){
				console.single = true;
			}
			console.runNewPoint(scale);
			console.drawLine();
			
			content.addChild(console);
			consoleAry.push(console);
			console.addEventListener(MouseEvent.CLICK,onConsoleClick);
			if(currentConsole){
				currentConsole.select = false;
			}
			currentConsole = console;
			console.x = maskSp.x + maskSp.width/2;
			console.y = maskSp.y + maskSp.height/2;
			console.addDel()
			return console;
		}
		private function onConsoleClick(event:MouseEvent):void{
			if(currentConsole){
				currentConsole.select = false;
			}
			var console:Console = event.currentTarget as Console;
			console.select = true;
			currentConsole = console;
			if(console.parent)
				content.setChildIndex(console,content.numChildren-1);
		}
		private function onLoaderClick(event:MouseEvent):void{
			if(currentConsole){
				currentConsole.select = false;
			}
		}
		public function set showedit(value:Boolean):void{
			content.visible = value;
			if(value == false){
				if(currentConsole){
					currentConsole.select = false;
				}
			}
		}
		public function get showedit():Boolean{
			return content.visible;
		}
		public function removeAll():void{
			while(content.numChildren){
				content.removeChildAt(0);
			}
		}
		public function drawBitmap():BitmapData{
			var bitmapdata:BitmapData = new BitmapData(this.width,this.height,false,0xff0000);
			var ma:Matrix = new Matrix();
			ma.scale(3,3);
			ma.translate(-400,-400);
			bitmapdata.draw(content,ma);
			var bitmap:Bitmap = new Bitmap(bitmapdata);
			//bitmap.x = -300;
			//bitmap.y = -300;
			this.addChild(bitmap);
			return bitmapdata;
		}
		public function getAllInfo():ByteArray{
			var allbyte:ByteArray = new ByteArray;
			allbyte.writeObject(data);
			for(var i:int;i<content.numChildren;i++){
				var console:Console = content.getChildAt(i) as Console;
				if(console){
					allbyte.writeObject(console.getAllInfo());
				}
			}
			return allbyte;
		}
		public function getAllInfoStr():String{
			var str:String = "";
			var base:String = encodeURI(data.name) + "^" + data.mark + "^" + data.x + "^" + data.y + "^" + data.w + "^" + data.h + "^"  + data.orgW + "^"  + data.orgH;
			str += base+"|";
			for(var i:int;i<content.numChildren;i++){
				var console:Console = content.getChildAt(i) as Console;
				if(console){
					//allbyte.writeObject(console.getAllInfo());
					str += console.getAllInfoString() + "|";
				}
			}
			str = str.substr(0,str.length-1);
			
			return str;
		}
		public function getImage():ByteArray{
			var rec:Rectangle = new Rectangle();
			for(var i:int=0;i<content.numChildren;i++){
				var console:Console = content.getChildAt(i) as Console;
				if(console){
					console.select = false;
					console.hideDel()
					if(console.targets.mask){
						rec.x = console.targets.mask.x;
						rec.y = console.targets.mask.y;
						rec.height = console.targets.mask.height;
						rec.width = console.targets.mask.width;
					}
					console.setMask(false);
				}
					
			}
			var bitmapdata:BitmapData = new BitmapData(444,444,true,0);
			bitmapdata.draw(content,null,null,null,rec);
			//var bitmap:Bitmap = new Bitmap(bitmapdata);
			//this.addChild(bitmap);
			for(i=0;i<content.numChildren;i++){
				console = content.getChildAt(i) as Console;
				if(console){
					console.setMask(true);
				}
				
			}
			
			var byte:ByteArray = PNGEncoder.encode(bitmapdata);
			return byte;
			
		}
	}
}